#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhctrlstate.h"


/*
 *
 */
int GetCtrlState(HWND hWnd)
{
    int ret = 0;
    MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
    if(p)
    {
        ret = p->state;
    }
    return ret;
}
/*
 *
 */
void SetCtrlState(HWND hWnd, int state)
{
    MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
    if(p)
    {
        p->state = state;
    }
}
/*
 *
 */
int GetCtrlType(HWND hWnd)
{
    int ret = 0;
    MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
    if(p)
    {
        ret = p->type;
    }
    return ret;
}
/*
 *
 */
void SetCtrlType(HWND hWnd, int type)
{
    MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
    if(p)
    {
        p->type = type;
    }
}
/*
 *
 */
static int MhCtrlStateProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case MSG_CREATE:
        {
        }
        break;
    case MSG_PAINT:
        {
            MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
            if(p)
            {
                if(p->type != MH_CTRL_TYPE_BMP)
                {
                    RECT rect;
                    GetClientRect(hWnd, &rect);
                    HDC hdc = GetClientDC(hWnd);
                    SetBkMode(hdc, BM_TRANSPARENT);
                    if(p->type == MH_CTRL_TYPE_SEL)
                    {
                        SetTextColor(hdc, COLOR_HQ_WHITE(hdc));
                    }
                    DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
                    ReleaseDC(hdc);
                }
            }
        }
        break;
    case MSG_LBUTTONDOWN:
        {
            MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
        	if (GetCapture() == hWnd)
        	{
        		break;
        	}
        	SetCapture(hWnd);
        	if(p)
        	{
        	    if(!(p->mode & MH_CTRL_CLICK_DISABLE))
        	    {
        	        p->state = !p->state;
        	    }
        	}
        	InvalidateRect(hWnd, NULL, TRUE);
        }
        break;
    case MSG_LBUTTONUP:
        {
        	if (GetCapture() != hWnd)
        	{
        		break;
        	}
        	ReleaseCapture();
        	HWND hParent = GetParent(hWnd);
        	int nID = GetDlgCtrlID(hWnd);
        	SendMessage(hParent, MSG_COMMAND, nID, 0);
        }
    	break;
    case MSG_DESTROY:
        {
            MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
            if(p && p->type == MH_CTRL_TYPE_BMP)
            {
                int i = 0;
                for(i = 0; i < MH_CTRL_STATE_MAX; ++i)
                {
                    UnloadBitmap(&p->bmp[i]);
                }
                FREE(p);
            }
        }
        break;
    case MSG_ERASEBKGND:
        {
            MH_CTRL_STATE *p = (MH_CTRL_STATE*)GetWindowAdditionalData(hWnd);
            if(p)
            {
                RECT rect;
                GetClientRect(hWnd, &rect);
                int nWidth = GET_RECT_WIDTH(rect);
                int nHeight = GET_RECT_HEIGHT(rect);
                HDC hdc = GetClientDC(hWnd);
                if(p->type == MH_CTRL_TYPE_BMP)
                {
                    //printf("ctrl_state: %d, %d, %d, %d\n", p->state, &p->bmp[p->state], nWidth, nHeight);
                    FillBoxWithBitmap(hdc, rect.left, rect.top, nWidth,
                            nHeight, &p->bmp[p->state]);
                }
                else if(p->type == MH_CTRL_TYPE_CODE)
                {
                    if(p->state)
                    {
                        SetBrushColor(hdc, COLOR_HQ_CYAN(hdc));
                    }
                    else
                    {
                        SetBrushColor(hdc, COLOR_HQ_BLUE(hdc));
                    }
                    FillBox(hdc, rect.left, rect.top, nWidth, nHeight);
                    SetPenColor(hdc, COLOR_HQ_GRAY(hdc));
                    Rectangle(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1);
                }
                else if(p->type == MH_CTRL_TYPE_YARN)
                {
                    if(p->state)
                    {
                        SetBrushColor(hdc, COLOR_HQ_GRAY(hdc));
                        FillBox(hdc, rect.left, rect.top, nWidth, nHeight);
                        SetPenColor(hdc, COLOR_HQ_BLACK(hdc));
                        MoveTo(hdc, rect.left, rect.bottom - 1);
                        LineTo(hdc, rect.left, rect.top);
                        LineTo(hdc, rect.right - 1, rect.top);
                        SetPenColor(hdc, COLOR_HQ_WHITE(hdc));
                        LineTo(hdc, rect.right - 1, rect.bottom - 1);
                        LineTo(hdc, rect.left, rect.bottom - 1);
                    }
                    else
                    {
                        SetBrushColor(hdc, COLOR_HQ_YELLOW(hdc));
                        FillBox(hdc, rect.left, rect.top, nWidth, nHeight);
                        SetPenColor(hdc, COLOR_HQ_WHITE(hdc));
                        MoveTo(hdc, rect.left, rect.bottom - 1);
                        LineTo(hdc, rect.left, rect.top);
                        LineTo(hdc, rect.right - 1, rect.top);
                        SetPenColor(hdc, COLOR_HQ_BLACK(hdc));
                        LineTo(hdc, rect.right - 1, rect.bottom - 1);
                        LineTo(hdc, rect.left, rect.bottom - 1);
                        SetPenColor(hdc, COLOR_HQ_WHITE(hdc));
                        MoveTo(hdc, rect.left + 1, rect.bottom - 2);
                        LineTo(hdc, rect.left + 1, rect.top + 1);
                        LineTo(hdc, rect.right - 2, rect.top + 1);
                        SetPenColor(hdc, COLOR_HQ_BLACK(hdc));
                        LineTo(hdc, rect.right - 2, rect.bottom - 2);
                        LineTo(hdc, rect.left + 1, rect.bottom - 2);
                    }
                }
                else if(p->type == MH_CTRL_TYPE_SEL || p->type == MH_CTRL_TYPE_SIGN ||
                    p->type == MH_CTRL_TYPE_2YARN || p->type == MH_CTRL_TYPE_CAM)
                {
                    if(p->state)
                    {
                        if(p->type == MH_CTRL_TYPE_SIGN)
                        {
                            SetBrushColor(hdc, COLOR_HQ_CYAN(hdc));
                        }
                        else if(p->type == MH_CTRL_TYPE_CAM)
                        {
                            SetBrushColor(hdc, COLOR_HQ_PINK(hdc));
                        }
                        else
                        {
                            SetBrushColor(hdc, COLOR_HQ_GRAY(hdc));
                        }
                        FillBox(hdc, rect.left, rect.top, nWidth, nHeight);
                        SetPenColor(hdc, COLOR_HQ_BLACK(hdc));
                        MoveTo(hdc, rect.left, rect.bottom - 1);
                        LineTo(hdc, rect.left, rect.top);
                        LineTo(hdc, rect.right - 1, rect.top);
                        SetPenColor(hdc, COLOR_HQ_WHITE(hdc));
                        LineTo(hdc, rect.right - 1, rect.bottom - 1);
                        LineTo(hdc, rect.left, rect.bottom - 1);
                    }
                    else
                    {
                        rect.left += 1;
                        rect.right -= 2;
                        rect.top += 1;
                        rect.bottom -= 2;
                        int nWidth = GET_RECT_WIDTH(rect);
                        int nHeight = GET_RECT_HEIGHT(rect);
                        if(p->type == MH_CTRL_TYPE_SEL || p->type == MH_CTRL_TYPE_CAM)
                        {
                            SetBrushColor(hdc, COLOR_HQ_2GREEN/*BKGND*/(hdc));
                        }
                        else
                        {
                            SetBrushColor(hdc, COLOR_HQ_RED(hdc));
                        }
                        if (lParam)
                        {
                            RECT rct;
                            memcpy(&rct, (RECT*) lParam, sizeof(RECT));
                            int w = GET_RECT_WIDTH(rct);
                            int h = GET_RECT_HEIGHT(rct);
                            FillBox(hdc, rct.left, rct.top, w, h);
                        }
                        else
                        {
                            FillBox(hdc, rect.left, rect.top, nWidth, nHeight);
                        }
                        SetPenColor(hdc, COLOR_HQ_BLACK(hdc));
                        MoveTo(hdc, rect.left, rect.bottom);
                        LineTo(hdc, rect.right, rect.bottom);
                        LineTo(hdc, rect.right, rect.top);
                        MoveTo(hdc, rect.left - 1, rect.bottom + 1);
                        LineTo(hdc, rect.right + 1, rect.bottom + 1);
                        LineTo(hdc, rect.right + 1, rect.top + 1);
                        SetPenColor(hdc, COLOR_HQ_WHITE(hdc));
                        MoveTo(hdc, rect.left, rect.bottom);
                        LineTo(hdc, rect.left, rect.top);
                        LineTo(hdc, rect.right, rect.top);
                        MoveTo(hdc, rect.left - 1, rect.bottom + 1);
                        LineTo(hdc, rect.left - 1, rect.top + 1);
                        LineTo(hdc, rect.right + 1, rect.top + 1);
                    }
                }
                ReleaseDC(hdc);
            }
        }
        return 0;
    }

    return DefaultControlProc (hWnd, message, wParam, lParam);
}
/*
 *
 */
BOOL RegisterMhCtrlState (void)
{
    WNDCLASS MyClass;

    MyClass.spClassName = MH_CTRL_STATE_NAME;
    MyClass.dwStyle     = WS_NONE;
    MyClass.dwExStyle   = WS_EX_NONE;
    MyClass.hCursor     = GetSystemCursor (IDC_ARROW);
    MyClass.iBkColor    = COLOR_lightwhite;
    MyClass.WinProc     = MhCtrlStateProc;
    return RegisterWindowClass (&MyClass);
}
/*
 *
 */
void UnregisterMhCtrlState (void)
{
    UnregisterWindowClass (MH_CTRL_STATE_NAME);
}
